//
//  CZStatusOriginalView.swift
//  weibo
//
//  Created by Apple on 16/3/16.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SnapKit

//  原创微博视图
class CZStatusOriginalView: UIView {
    //  记录当前视图的底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: CZStatusViewModel? {
        didSet {
            if let userImageUrl =  statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImageWithURL(NSURL(string: userImageUrl), placeholderImage: UIImage(named: "avatar_default"))
            }
            userNameLabel.text = statusViewModel?.status?.user?.screen_name
            timeLabel.text = statusViewModel?.timeContent
            sourceLabel.text = statusViewModel?.sourceContent
            //  会员等级以及认证等级
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedLevelImageView.image = statusViewModel?.verifiedLevelImage
            //  设置原创微博内容
            //contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributeStr
            contentLabel.matchResults = statusViewModel?.originalMatchResults
            //  卸载约束
            bottomConstraint?.uninstall()
            //  更新约束
            if let picurls = statusViewModel?.status?.pic_urls where picurls.count > 0 {
                //  1. 显示图片
                pictureView.hidden = false
                //  2. 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
                })
                
                //  3. 设置数据
                pictureView.picUrls = picurls
            } else {
                //  1. 显示图片
                pictureView.hidden = true
                //  2. 更新约束
                snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    // MARK:    懒加载控件
    private lazy var userImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    private lazy var userNameLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: nameLabelFontSize)
    
    private lazy var mbrankImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    private lazy var verifiedLevelImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var timeLabel = UILabel(textColor: UIColor.orangeColor(), fontSize: timeLabelFontSize)
    private lazy var sourceLabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: timeLabelFontSize)
    //  微博内容
    private lazy var contentLabel: CZStatusLabel = {
        let label = CZStatusLabel(textColor: UIColor.darkGrayColor(), fontSize: contentLabelFontSize)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureView: CZStatusPictureView = {
        let collectionView = CZStatusPictureView()
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //  添加控件
        addSubview(userImageView)
        addSubview(userNameLabel)
        addSubview(mbrankImageView)
        addSubview(verifiedLevelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //  添加约束
        
        userImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusCellMargin)
            make.leading.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        userNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusCellMargin)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userNameLabel.snp_trailing).offset(StatusCellMargin)
        }
        verifiedLevelImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(StatusCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(userImageView.snp_bottom).offset(StatusCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.leading.equalTo(contentLabel)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //  添加关键约束,原创微博的高度等于 contentLabel的底部加上一个间距
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(StatusCellMargin).constraint
        }
        
    }
    
    
    
}
